class DiscsController < ApplicationController
  #If AJAX request, use the slim layout (no header or footer)
  #Otherwise use the main layout
  layout Proc.new { |controller| controller.request.xhr? ? 'slim' : 'main' }

  #GET /release/:release_id/edition/:edition_id/disc/new
  def new
    #Generates a new disc and 5 empty songs to go with it
    @edition = Edition.find(params[:edition_id])
	@disc = @edition.discs.new
	5.times { @disc.songs.build }
	
	respond_to do |format|
	  format.html {}
	  format.js {}
	end
  end
  
  #
  #POST /release/:release_id/eidtion/:edition_id/disc
  def create
    @edition = Edition.find(params[:edition_id])
	
	respond_to do |format|
	
	  #User clicked the save button
	  if params[:commit]
	  
	    #Add songs to the disc
	    @disc = @edition.discs.build(params[:disc])
		
		#Save 
	    if @disc.save
	      format.html { redirect_to edit_release_edition_path(@edition.release, @edition) }
	      format.js {
	        render :update do |page|
			  page.replace_html("track-pane", :partial => 'editions/track_pane',
			                                  :locals => { :release => @edition.release, :edition => @edition })
		    end
  	      }
		  
		#There was some sort of error saving  
	    else
		  format.html { render :action => :new }
	      format.js {
		    render :update do |page|
		      page.replace_html("form-errors", @disc.errors.full_messages)
		    end
		  }
	    end
		
	  #User clicked the cancel button	
	  else
	    format.html { redirect_to edit_release_edition_path(@edition.release, @edition) }
	    format.js {
		  render :update do |page|
		    page.replace_html("track-pane", :partial => 'editions/track_pane',
			                                :locals => { :release => @edition.release, :edition => @edition })
		  end
		}
	  end
	end
  end
  
  #
  #
  def edit
    @disc = Disc.find(params[:id])
	@edition = @disc.edition
	
	respond_to do |format|
	  format.html { }
	  format.js {
	    render :update do |page|
	      page.replace_html("edit-disc-#{@disc.id}", :partial => 'disc_form',
		                                             :locals => {:edition => @edition, :disc => @disc})
		end
	  }
	end
  end
  
  #
  #
  def update
    @disc = Disc.find(params[:id])
	
	respond_to do |format|
	
	  #User clicked the update button
	  if params[:commit]
	  
	    #Save changes
	    if @disc.update_attributes(params[:disc])
		  format.html { }
	      format.js {
		    render :update do |page|
		      page.replace_html("edit-disc-#{@disc.id}", :partial => 'edit_disc',
			                                             :locals => {:disc => @disc})
		    end
		  }
		  
		#There was an error saving  
	    else
	      format.html { }
		  format.js {
		    render :update do |page|
			  page.replace_html("form-errors", @disc.errors.full_messagse)
			end
		  }
	    end
		
	  #User clicked the cancel button, replace the form with the normal view	
	  else
	    format.html { }
	    format.js {
		  render :update do |page|
		    page.replace_html("edit-disc-#{@disc.id}", :partial => 'edit_disc',
			                                          :locals => {:disc => @disc})
		  end
		}
	  end
	end
  end
  
  #
  #
  def destroy

  end

end
